package com.niit.yhserver.controller;


import com.niit.yhpojo.dto.MenuDTO;
import com.niit.yhpojo.entity.Menu;
import com.niit.yhserver.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/menu")
@Slf4j
@CrossOrigin("*")
// 添加跨域支持

public class MenuController {

    @Autowired
    private MenuService menuService;
    /*
    * 获取全部的菜品
    * */
    @GetMapping("/getAll")
    public List<Menu> getAllMenu(){

        return menuService.getAllMenu();
    }

    @GetMapping("/getById/{id}")
    public Menu getMenuById(@PathVariable("id") Integer id){

        return menuService.getMenuById(id);
    }

    /*
    * 添加菜品
    * @param menuDTO
    * @return
    * */
    @PostMapping("/addMenu")
    public String addMenu(@RequestBody MenuDTO menuDTO){
        log.info("新增菜品请求：foodid={}, foodname={}, foodprice={},foodtype={},fooddesc={},foodstatus={}",
                menuDTO.getFoodid(),
                menuDTO.getFoodname(),
                menuDTO.getFoodprice(),
                menuDTO.getFoodtype(),
                menuDTO.getFooddesc(),
                menuDTO.getFoodstatus()
        );
        try {
            menuService.addMenu(menuDTO);
            return "新增菜品成功";
        }
        catch (Exception e) {
            log.error("新增菜品失败", e);
            return "新增菜品失败: " + e.getMessage();
        }
    }

    /*
    * 修改菜品
    * @param menu
    * @return
    * */
    @PutMapping("/updateMenu")
    public String updateMenu(@RequestBody MenuDTO menuDTO){
        log.info("更新菜品请求: foodid={}, foodname={}, foodprice={},foodtype={},fooddesc={},foodstatus={}",
                menuDTO.getFoodid(),
                menuDTO.getFoodname(),
                menuDTO.getFoodprice(),
                menuDTO.getFoodtype(),
                menuDTO.getFooddesc(),
                menuDTO.getFoodstatus()

        );

        try {
            menuService.updateMenu(menuDTO);
            return "编辑菜品成功";
        } catch (Exception e) {
            log.error("更新菜品失败", e);
            return "编辑菜品失败: " + e.getMessage();
        }
    }

    /*
    * 删除菜品
    * @param id
    * @return
    * */
    @DeleteMapping("/deleteMenu/{id}")
    public String deleteMenu(@PathVariable("id") Integer id) {
        menuService.deleteMenuById(id);
        return "下架菜品成功";

    }




}
